package com.student.manager.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtil {

    static ComboPooledDataSource dataSource = null;

    static {
        dataSource = new ComboPooledDataSource();
    }

    public static DataSource getDataSouce() {
        return dataSource;
    }

    /**
     * 获取连接对象
     *
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {

        return dataSource.getConnection();
    }

    public static void release(Connection connection, Statement statement, ResultSet resultSet
    ) {
        closeConnection(connection);
        closeStatement(statement);
        closeResultSet(resultSet);

    }

    public static void release(Connection connection, Statement statement) {

    }

    private static void closeResultSet(ResultSet resultSet) {

        try {
            if (resultSet != null) {

                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            resultSet = null;
        }
    }

    private static void closeStatement(Statement statement) {
        try {

            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            statement = null;
        }
    }

    private static void closeConnection(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            connection = null;
        }
    }


}
